home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / language / harvest.cpt / Harvest C / Examples / Harvest MiniEdit / BuggyEdit.c next >
Encoding:
C/C++ Source or Header  |  1992-03-20  |  6.8 KB  |  341 lines

  1. /*********************************************************************
  2.  
  3.     Buggy MiniEdit.c
  4.     
  5.     The sample application from Inside Macintosh (RoadMap p.15-17)
  6.     beefed up a bit by Stephen Z. Stein, Symantec Corp.
  7.     Use this file with the ╥MiniEdit╙ chapter of your manual.
  8.     
  9.     The resources used in this program are in the file MiniEdit.╣.rsrc.
  10.     
  11.     In order for THINK C to find the resource file for this
  12.     project, be sure you╒ve named the project MiniEdit.╣
  13.     
  14.     
  15. *********************************************************************/
  16.  
  17. #include <Windows.h>
  18. #include <Menus.h>
  19. #include <Resources.h>
  20. #include <Events.h>
  21. #include <Dialogs.h>
  22. #include <Desk.h>
  23. #include <Scrap.h>
  24. #include <OSEvents.h>
  25. #include <Memory.h>
  26. #include <Fonts.h>
  27. #include <TextEdit.h>
  28. #include <ToolUtils.h>
  29. #include <Errors.h>
  30. #include <Files.h>
  31. #include "mini.file.h"
  32. #include "mini.windows.h"
  33. #include "MiniEdit.h"
  34.  
  35.  
  36.  
  37. WindowRecord    wRecord;
  38. WindowPtr        myWindow;
  39. TEHandle        TEH;
  40. int                linesInFolder;
  41. Rect            dragRect = { 0, 0, 1024, 1024 };
  42. MenuHandle        myMenus[3];
  43. ControlHandle     vScroll;
  44. Cursor            editCursor;
  45. Cursor            waitCursor;
  46. char            dirty;
  47.  
  48. extern Str255     theFileName;
  49.  
  50. #define    ours(w)        ((myWindow != NULL) && (w == myWindow))
  51.  
  52.  
  53. main() 
  54. {
  55.     int        myRsrc;
  56.     
  57.     Debugger();
  58.     
  59.     InitGraf(&(qd.thePort));
  60.     InitFonts();
  61.     FlushEvents(everyEvent, 0);
  62.     InitWindows();
  63.     InitMenus();
  64.     TEInit();
  65.     InitDialogs(0L);
  66.     InitCursor();
  67.     MaxApplZone();
  68.  
  69. /*
  70. /*  The following statement is included as a check to see if we can
  71. /*    access our program's resources.  When the project is run from
  72. /*    THINK C, the resource file <project name>.rsrc is automatically
  73. /*  opened.  When an application is built, these resources are 
  74. /*     automatically merged with the application.
  75. /*
  76. */
  77.     
  78.     if (GetResource('MENU', fileID)==0) {
  79.         SysBeep(20);
  80.         CantOpen();
  81.         return 0;
  82.     }
  83.     
  84.     SetUpFiles();
  85.     SetUpCursors();
  86.     SetUpMenus();
  87.     SetUpWindows();
  88.     while (MainEvent()) ;
  89. }
  90.  
  91.  
  92.  
  93. int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  94.  
  95. {
  96.     switch (windowPart) {
  97.         case inGoAway:
  98.             if (ours(whichWindow))
  99.                 if (TrackGoAway(myWindow, myEvent->where))
  100.                     DoFile(fmClose);
  101.             break;
  102.  
  103.         case inMenuBar:
  104.             return(DoCommand(MenuSelect(myEvent->where)));
  105.  
  106.         case inSysWindow:
  107.             SystemClick(myEvent, whichWindow);
  108.             break;
  109.  
  110.         case inDrag:
  111.             if (ours(whichWindow))
  112.                 DragWindow(whichWindow, myEvent->where, &dragRect);
  113.             break;
  114.  
  115.         case inGrow:
  116.             if (ours(whichWindow))
  117.                 MyGrowWindow(whichWindow, myEvent->where);
  118.             break;
  119.  
  120.         case inContent:
  121.             if (whichWindow != FrontWindow())
  122.                 SelectWindow(whichWindow);
  123.             else if (ours(whichWindow))
  124.                 DoContent(whichWindow, myEvent);
  125.             break;
  126.     }
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. int MainEvent(void) 
  135. {
  136.     EventRecord        myEvent;
  137.     WindowPtr        whichWindow;
  138.     short            windowPart;
  139.     Rect            r;
  140.     
  141.     MaintainCursor();
  142.     MaintainMenus();
  143.     SystemTask();
  144.     TEIdle(TEH);
  145.     if (GetNextEvent(everyEvent, &myEvent)) {
  146.         switch (myEvent.what) {
  147.         case mouseDown:
  148.             windowPart = FindWindow(myEvent.where, &whichWindow);
  149.             DoMouseDown(windowPart, whichWindow, &myEvent);
  150.             break;
  151.  
  152.         case keyDown:
  153.         case autoKey: 
  154.             {
  155.             register char    theChar;
  156.             
  157.             theChar = myEvent.message & charCodeMask;
  158.             if ((myEvent.modifiers & cmdKey) != 0) 
  159.                 return(DoCommand(MenuKey( theChar)));
  160.             else {
  161.                 TEKey(theChar, TEH);
  162.                 ShowSelect();
  163.                 dirty = 1;
  164.                 }
  165.             }
  166.             break;
  167.  
  168.         case activateEvt:
  169.             if (ours((WindowPtr)myEvent.message)) {
  170.                 if (myEvent.modifiers & activeFlag) {
  171.                     TEActivate(TEH);
  172.                     ShowControl(vScroll);
  173.                     DisableItem(myMenus[editM], undoCommand);
  174.                     TEFromScrap();
  175.                 }
  176.                 else {
  177.                     TEDeactivate(TEH);
  178.                     HideControl(vScroll);
  179.                     ZeroScrap();
  180.                     TEToScrap();
  181.                 }
  182.             }
  183.             break;
  184.  
  185.         case updateEvt: 
  186.             if (ours((WindowPtr) myEvent.message))
  187.                     UpdateWindow(myWindow);
  188.             break;
  189.         } /* end of case myEvent.what */
  190.     } /* if */
  191.     return(1);
  192. }
  193.  
  194. int SetUpMenus(void)
  195. {
  196.     int        i;
  197.     
  198.     myMenus[appleM] = NewMenu(appleID, "\p\024");
  199.     AddResMenu(myMenus[appleM], 'DRVR');
  200.     myMenus[fileM] = GetMenu(fileID);
  201.     myMenus[editM] = GetMenu(editID);
  202.     for ((i=appleM); (i<=editM); i++)
  203.         InsertMenu(myMenus[i], 0) ;
  204.     DrawMenuBar();
  205. }
  206.  
  207. int DoCommand(long mResult)
  208.  
  209. {
  210.     int        theItem;
  211.     Str255    name;
  212.     
  213.     theItem = LoWord(mResult);
  214.     switch (HiWord(mResult)) {
  215.         case appleID:
  216.             GetItem(myMenus[appleM], theItem, &name);
  217.             OpenDeskAcc(name);
  218.             SetPort(myWindow);
  219.             break;
  220.  
  221.         case fileID: 
  222.             DoFile(theItem);
  223.             break;
  224.  
  225.         case editID: 
  226.             if (SystemEdit(theItem-1) == 0) {
  227.                 switch (theItem) {
  228.                     case cutCommand:
  229.                         TECut(TEH);
  230.                         dirty = 1;
  231.                         break;
  232.     
  233.                     case copyCommand:
  234.                         TECopy(TEH);
  235.                         break;
  236.         
  237.                     case pasteCommand:
  238.                         TEPaste(TEH);
  239.                         dirty = 1;
  240.                         break;
  241.         
  242.                     case clearCommand:
  243.                         TEDelete(TEH);
  244.                         dirty = 1;
  245.                         break;
  246.                 }
  247.                 ShowSelect();
  248.             }
  249.             break;
  250.     }
  251.     HiliteMenu(0);
  252.     return(1);
  253. }
  254.  
  255. int MaintainCursor(void)
  256. {
  257.     Point        pt;
  258.     WindowPeek    wPtr;
  259.     GrafPtr        savePort;
  260.     
  261.     if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
  262.         GetPort(&savePort);
  263.         SetPort((GrafPtr)wPtr);
  264.         GetMouse(&pt);
  265.         if (PtInRect(pt, &(**TEH).viewRect ) )
  266.             SetCursor( &editCursor);
  267.         else SetCursor(&qd.arrow);
  268.         SetPort(savePort);
  269.     }
  270. }
  271.  
  272. int MaintainMenus(void)
  273. {
  274.     if ( !(*(WindowPeek)myWindow).visible || 
  275.             !ours(FrontWindow()) ) {
  276.         EnableItem(myMenus[fileM], fmNew);
  277.         EnableItem(myMenus[fileM], fmOpen);
  278.         DisableItem(myMenus[fileM], fmClose);
  279.         DisableItem(myMenus[fileM], fmSave);
  280.         DisableItem(myMenus[fileM], fmSaveAs);
  281.         DisableItem(myMenus[fileM], fmRevert);
  282.         DisableItem(myMenus[fileM], fmPrint);
  283.         EnableItem(myMenus[editM], undoCommand);
  284.         EnableItem(myMenus[editM], cutCommand);
  285.         EnableItem(myMenus[editM], copyCommand);
  286.         EnableItem(myMenus[editM], clearCommand);
  287.     }
  288.     else {
  289.         DisableItem(myMenus[fileM], fmNew);
  290.         DisableItem(myMenus[fileM], fmOpen);
  291.         EnableItem(myMenus[fileM], fmClose);
  292.         EnableItem(myMenus[fileM], fmSaveAs);
  293.         EnableItem(myMenus[fileM], fmPrint);
  294.         if (dirty && theFileName[0] != 0) {
  295.             EnableItem(myMenus[fileM], fmRevert);
  296.             EnableItem(myMenus[fileM], fmSave);
  297.         }
  298.         else {
  299.             DisableItem(myMenus[fileM], fmRevert);
  300.             DisableItem(myMenus[fileM], fmSave);
  301.         }
  302.         DisableItem(myMenus[editM], undoCommand);
  303.         if ((**TEH).selStart==(**TEH).selEnd) {
  304.             DisableItem(myMenus[editM], cutCommand);
  305.             DisableItem(myMenus[editM], copyCommand);
  306.             DisableItem(myMenus[editM], clearCommand);
  307.         }
  308.         else {
  309.             EnableItem(myMenus[editM], cutCommand);
  310.             EnableItem(myMenus[editM], copyCommand);
  311.             EnableItem(myMenus[editM], clearCommand);
  312.         }
  313.     }
  314. }
  315.  
  316. int SetUpCursors(void)
  317. {
  318.     CursHandle    hCurs;
  319.     
  320.     hCurs = GetCursor(1);
  321.     editCursor = **hCurs;
  322.     hCurs = GetCursor(watchCursor);
  323.     waitCursor = **hCurs;
  324. }
  325.  
  326.  
  327. int CantOpen(void)
  328. {
  329.     Rect r;
  330.  
  331.     SetRect(&r, 152, 60, 356, 132);
  332.     SetPort((myWindow = NewWindow( (Ptr) 0L, &r, "\p", true, dBoxProc, (WindowPtr) -1L, false, 0L)));
  333.     TextFont(0);
  334.     MoveTo(4, 20);
  335.     DrawString("\pCan't open resource file.");
  336.     MoveTo(4, 40);
  337.     DrawString("\pClick mouse to exit.");
  338.     do {
  339.     } while (!Button());
  340. }
  341.